﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockHelper.Scripts.Comm
{
    class Time
    {



        /// <summary>
        /// 生成时间戳
        /// </summary>
        /// <returns>10位时间戳</returns>
        public static int GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
            return Convert.ToInt32(ts.TotalSeconds);
        }
        /// <summary>
        /// 生成时间戳
        /// </summary>
        /// <returns>10位时间戳</returns>
        public static int GetTimeStamp(DateTime date)
        {
            TimeSpan ts = date - new DateTime(1970, 1, 1);
            return Convert.ToInt32(ts.TotalSeconds);
        }
        
        /// <summary>
        /// 时间戳转dete
        /// </summary>
        /// <param name="timeStamp">10位时间戳</param>
        /// <returns>DateTime格式</returns>
        public static DateTime GetDateTime(int timeStamp = -1)
        {
            if (timeStamp == -1) timeStamp = GetTimeStamp();
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = ((long)timeStamp * 10000000);
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(toNow);
            return targetDt;
        }

        /// <summary>
        /// 获得date字符串
        /// </summary>
        /// <param name="timeStamp">10位时间戳</param>
        /// <param name="format">格式 默认为 yyyy-MM-dd HH:mm:ss</param>
        /// <returns>字符串时间戳</returns>
        public static string GetDate(int timeStamp = -1, string format = "yyyy-MM-dd HH:mm:ss")
        {
            DateTime date = GetDateTime(timeStamp);
            return date.ToString(format);
        }

        public static DateTime GetDateUp(int s) {
            return new DateTime().AddSeconds(s);
        }

        public static int GetStartTime()
        {
            DateTime now = DateTime.Now;
            return GetTimeStamp(new DateTime(now.Year, now.Month, now.Day));
        }

        public static int GetWeek()
        {
            return (int)DateTime.Now.DayOfWeek;
        }

    }
}
